package cn.andy.android.crm.fragment;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import cn.andy.android.crm.R;
import cn.andy.android.crm.adapter.MyAdapter;
import cn.andy.android.crm.beans.PersonInfo;
import cn.andy.android.crm.common.Util;
import cn.andy.android.crm.db.PersonDBHelper;

public class SQLiteDemoFragment extends Fragment{
	
	private PersonDBHelper helper;
	private View view;
	private ListView listview;
	private MyAdapter adapter;
	private PersonInfo currperson;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		view = inflater.inflate(R.layout.fragment_sqlitedemo, container, false);
		
		helper = new PersonDBHelper(getActivity());
		
		loadData2ListView();
		
		initComp();
		
		return view;
	}
	
	private void loadData2ListView(){
		Cursor cursor = helper.select();
		List<PersonInfo> persons = helper.get(cursor);
		
		adapter = new MyAdapter(getActivity(), persons, helper);
		listview = (ListView) view.findViewById(R.id.sqlitedemo_listview);
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(clickListener);
		listview.setOnItemLongClickListener(longclickListener);
	}
	
	OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			adapter.selectedpos = position;
			adapter.notifyDataSetInvalidated();
			PersonInfo p = adapter.arrays.get(position);
			
			loadPerson(p);
			currperson = p;
			
		}
			
	};
	
	OnItemLongClickListener longclickListener = new AdapterView.OnItemLongClickListener() {

		public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			adapter.selectedpos = position;
			adapter.notifyDataSetInvalidated();
			PersonInfo p = adapter.arrays.get(position);
			
			loadPerson(p);
			currperson = p;
			return true;
		}
			
	};
	
	private void initComp(){
		final EditText etname = (EditText) view.findViewById(R.id.sqlitedemo_name);
		final EditText etemail = (EditText) view.findViewById(R.id.sqlitedemo_email);
		final EditText etaddress = (EditText) view.findViewById(R.id.sqlitedemo_address);
		
		final Button btnSave = (Button) view.findViewById(R.id.save);
		btnSave.setOnClickListener(new Button.OnClickListener() {
			
			public void onClick(View v) {
				String name = etname.getText().toString();
				String email = etemail.getText().toString();
				String address = etaddress.getText().toString();
				if(Util.isNullStr(name)){
					Toast.makeText(getActivity(), "Please Input Name", Toast.LENGTH_LONG).show();
				}
				else if(Util.isNullStr(email)){
					Toast.makeText(getActivity(), "Please Input Email", Toast.LENGTH_LONG).show();
				}
				else if(Util.isNullStr(address)){
					Toast.makeText(getActivity(), "Please Input Address", Toast.LENGTH_LONG).show();
				}
				else{
					if(currperson == null){
						PersonInfo p = new PersonInfo();
						p.setName(name);
						p.setEmail(email);
						p.setAddress(address);
						helper.insert(p);
						etname.setText(null);
						etemail.setText(null);
						etaddress.setText(null);
	
						Cursor cursor = helper.select();
						List<PersonInfo> persons = helper.get(cursor);
						adapter.arrays = persons;
						adapter.notifyDataSetChanged();
					} else {
						currperson.setName(name);
						currperson.setEmail(email);
						currperson.setAddress(address);
						helper.update(currperson, currperson.getId());
						currperson = null;
						etname.setText(null);
						etemail.setText(null);
						etaddress.setText(null);
	
						Cursor cursor = helper.select();
						List<PersonInfo> persons = helper.get(cursor);
						adapter.arrays = persons;
						adapter.notifyDataSetChanged();
					}
				}
			}
		});
	}

	private void loadPerson(PersonInfo p){
		EditText etname = (EditText) view.findViewById(R.id.sqlitedemo_name);
		EditText etemail = (EditText) view.findViewById(R.id.sqlitedemo_email);
		EditText etaddress = (EditText) view.findViewById(R.id.sqlitedemo_address);
			
		etname.setText(p.getName());
		etemail.setText(p.getEmail());
		etaddress.setText(p.getAddress());
	}

}
